﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SSDTConditions;
using System.Data;

namespace UnitTestProject
{
    [TestClass]
    public class StringValueConditionUnitTest
    {
        [TestMethod]
        public void StringNullValueTestMethod()
        {
            // Arrange
            StringValueCondition condition = new StringValueCondition();
            StringValueCondition.StringValueVerificationCode expected;
            StringValueCondition.StringValueVerificationCode actual;

            DataTable table = new DataTable();
            ArrangeTestConditionTable(table);

            // Null Expected condition is true
            condition.NullExpected = true;
            table.Rows[0][0] = DBNull.Value;
            expected = StringValueCondition.StringValueVerificationCode.Ok;
            // Act 
            actual = condition.StringConditionVerification(table);
            // Assert
            Assert.AreEqual(expected, actual);

            // Null Expected condition is false
            condition.NullExpected = true;
            table.Rows[0][0] = "test";
            expected = StringValueCondition.StringValueVerificationCode.NullExptected;
            // Act 
            actual = condition.StringConditionVerification(table);
            // Assert
            Assert.AreEqual(expected, actual);
        }

        [TestMethod]
        public void StringConditionTestMethod()
        {
            // Arrange
            StringValueCondition condition = new StringValueCondition();
            StringValueCondition.StringValueVerificationCode expected;
            StringValueCondition.StringValueVerificationCode actual;

            DataTable table = new DataTable();
            ArrangeTestConditionTable(table);

            // Equals condition is true
            condition.Comparison = StringValueCondition.ComparisonType.Equals;
            condition.ExpectedValue = "test";
            table.Rows[0][0] = "test";
            expected = StringValueCondition.StringValueVerificationCode.Ok;
            // Act 
            actual = condition.StringConditionVerification(table);
            // Assert
            Assert.AreEqual(expected, actual);

            // Equals condition is false
            condition.Comparison = StringValueCondition.ComparisonType.Equals;
            condition.ExpectedValue = "test";
            table.Rows[0][0] = "text";
            expected = StringValueCondition.StringValueVerificationCode.FalseCondition;
            // Act 
            actual = condition.StringConditionVerification(table);
            // Assert
            Assert.AreEqual(expected, actual);

            // Contains condition is true
            condition.Comparison = StringValueCondition.ComparisonType.Contains;
            condition.ExpectedValue = "test";
            table.Rows[0][0] = "this is a test case";
            expected = StringValueCondition.StringValueVerificationCode.Ok;
            // Act 
            actual = condition.StringConditionVerification(table);
            // Assert
            Assert.AreEqual(expected, actual);

            // Contains condition is false
            condition.Comparison = StringValueCondition.ComparisonType.Contains;
            condition.ExpectedValue = "text";
            table.Rows[0][0] = "this is a test case";
            expected = StringValueCondition.StringValueVerificationCode.FalseCondition;
            // Act 
            actual = condition.StringConditionVerification(table);
            // Assert
            Assert.AreEqual(expected, actual);

            // Not Contains condition is true
            condition.Comparison = StringValueCondition.ComparisonType.NotContains;
            condition.ExpectedValue = "text";
            table.Rows[0][0] = "this is a test case";
            expected = StringValueCondition.StringValueVerificationCode.Ok;
            // Act 
            actual = condition.StringConditionVerification(table);
            // Assert
            Assert.AreEqual(expected, actual);

            // Not Contains condition is false
            condition.Comparison = StringValueCondition.ComparisonType.NotContains;
            condition.ExpectedValue = "test";
            table.Rows[0][0] = "this is a test case";
            expected = StringValueCondition.StringValueVerificationCode.FalseCondition;
            // Act 
            actual = condition.StringConditionVerification(table);
            // Assert
            Assert.AreEqual(expected, actual);

            // StartsWith condition is true
            condition.Comparison = StringValueCondition.ComparisonType.StartsWith;
            condition.ExpectedValue = "this";
            table.Rows[0][0] = "this is a test case";
            expected = StringValueCondition.StringValueVerificationCode.Ok;
            // Act 
            actual = condition.StringConditionVerification(table);
            // Assert
            Assert.AreEqual(expected, actual);

            // StartsWith condition is false
            condition.Comparison = StringValueCondition.ComparisonType.StartsWith;
            condition.ExpectedValue = "test";
            table.Rows[0][0] = "this is a test case";
            expected = StringValueCondition.StringValueVerificationCode.FalseCondition;
            // Act 
            actual = condition.StringConditionVerification(table);
            // Assert
            Assert.AreEqual(expected, actual);

            // EndsWith condition is true
            condition.Comparison = StringValueCondition.ComparisonType.EndsWith;
            condition.ExpectedValue = "case";
            table.Rows[0][0] = "this is a test case";
            expected = StringValueCondition.StringValueVerificationCode.Ok;
            // Act 
            actual = condition.StringConditionVerification(table);
            // Assert
            Assert.AreEqual(expected, actual);

            // EndsWith condition is false
            condition.Comparison = StringValueCondition.ComparisonType.EndsWith;
            condition.ExpectedValue = "test";
            table.Rows[0][0] = "this is a test case";
            expected = StringValueCondition.StringValueVerificationCode.FalseCondition;
            // Act 
            actual = condition.StringConditionVerification(table);
            // Assert
            Assert.AreEqual(expected, actual);

            // Equals condition is true when case insensitive
            condition.Comparison = StringValueCondition.ComparisonType.Equals;
            condition.ExpectedValue = "test";
            condition.CaseSensitive = false;
            table.Rows[0][0] = "TEst";
            expected = StringValueCondition.StringValueVerificationCode.Ok;
            // Act 
            actual = condition.StringConditionVerification(table);
            // Assert
            Assert.AreEqual(expected, actual);
        }

        private static void ArrangeTestConditionTable(DataTable table)
        {
            // add first column
            table.Columns.Add("1", typeof(string));
            // add first row
            DataRow row = table.NewRow();
            table.Rows.Add(row);
        }
    }
}
